package com.jn.okhttp.presenter;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;

import androidx.annotation.NonNull;

import com.google.gson.Gson;
import com.jn.okhttp.common.WeakRrfHandler;
import com.jn.okhttp.contract.IHotSearchContact;
import com.jn.okhttp.entity.HotSearch;
import com.jn.okhttp.service.HotSearchService;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class HotSearchPresenter implements IHotSearchContact.HotSearchPresenter {
    private static final int HTTP_SUCCESS = 1;
    public static final int HTTP_SUCCESS2 = 2;
    private static final int HTTP_FAIL = 0;
    private IHotSearchContact.HotSearchView mMainView;
    private Context mContext;
    private OkHttpClient mClient;

    public HotSearchPresenter(IHotSearchContact.HotSearchView mMainView, Context mContext) {
        this.mMainView = mMainView;
        this.mContext = mContext;
        mClient = new OkHttpClient();
    }

    @Override
    public void getData(String keyWord) {
        // Retrofit获取方式
        String url = "https://api.oioweb.cn/api/";
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(url)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        HotSearchService hotSearchService = retrofit.create(HotSearchService.class);

        hotSearchService.getData6(keyWord).enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(retrofit2.Call<ResponseBody> call, Response<ResponseBody> response) {
                try {
                    String body = response.body().string();
                    String url = call.request().url().toString();
                    Message message = Message.obtain();
                    message.obj = new String[]{body, url};
                    message.what = HTTP_SUCCESS;
                    mHandler.sendMessage(message);
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {

            }
        });
    }

    WeakRrfHandler<HotSearchPresenter> mHandler = new WeakRrfHandler<>(Looper.getMainLooper(), this, new Handler.Callback() {
        @Override
        public boolean handleMessage(@NonNull Message msg) {
            int result = msg.what;
            switch (result) {
                case HTTP_SUCCESS:
                    String[] data = (String[]) msg.obj;
                    if (data == null || data.length < 2) {
                        return false;
                    }
                    String jsonStr = data[0];
                    Gson gson = new Gson();
                    HotSearch hotSearchEntity = gson.fromJson(jsonStr, HotSearch.class);
                    if (mMainView != null) {
                        mMainView.showData(hotSearchEntity);
                    }
                    break;
                case HTTP_SUCCESS2:
                    if (mMainView != null) {
                        mMainView.showData((HotSearch) msg.obj);
                    }
                    break;
                case HTTP_FAIL:
                    break;
            }
            return false;
        }
    });
}